package ipad.asia.controller;


import ipad.asia.entity.Consumer;
import ipad.asia.service.consumer.ConsumerFeign;
import ipad.asia.service.product.ProductFeign;
import ipad.asia.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class UserInfoController {
    @Autowired
    private ConsumerFeign consumerFeign;
    @RequestMapping(value = "/userInfo")
    public String pay(HttpServletRequest request, Model model,
                      @RequestParam(value = "phone",required = false) String phone,
                      @RequestParam(value = "email",required = false) String email,
                      @RequestParam(value = "sex",required = false) String sex,
                      @RequestParam(value = "address",required = false)String address,
                      @RequestParam(value = "signature",required = false) String signature){
        HttpSession session=request.getSession();
        String user= (String) session.getAttribute("user");
        if(phone==null&&email==null&&sex==null&&signature==null&&address==null){
            Consumer consumer = consumerFeign.getName(user);
            model.addAttribute("consumer",consumer);
            return "userInfo";
        }
        else if(sex!=null){
            Consumer consumer = consumerFeign.getName(user);
            consumerFeign.updateInfo(user,phone,email,sex,address,signature);
            model.addAttribute("consumer",consumer);
            return "userInfo";
        }
        else {
            Consumer consumer = consumerFeign.getName(user);
            consumerFeign.updateInfo(user,phone,email,consumer.getSex(),address,signature);
            model.addAttribute("consumer",consumer);
            return "userInfo";}
    }

}
